<?php

/**
 * 后台用户相关model
 */
namespace backend\models;

use Yii;
use yii\db\ActiveRecord;
use yii\data\Pagination;
use yii\data\ArrayDataProvider;

use backend\models\User;
use backend\models\novel\Novel;
use backend\models\novel\NovelChapter;

class UserConsume extends ActiveRecord
{
    
    /**
     * 根据用户id获取用户记录
     */
    public function getConsumeList( $params = array() )
    {
        $consume = self::find();
        $consume->select('*');
        $page = $params['page'];
        $offset = 0;
        $limit = $params['pageSize'];
        if($page > 0){
            $offset = ($page - 1) * $limit;
        }
        if(isset($params['start']) && $params['start']){
            $consume->andWhere("pay_time >= :st");
            $consume->addParams(array(":st"=>$params['start']));
        }
        if(isset($params['end']) && $params['end']){
            $consume->andWhere("pay_time <= :et");
            $consume->addParams(array(":et"=>$params['end']));
        }
        $consume->andWhere("user_id = :id");
        $consume->addParams(array(":id"=>$params['user_id']));
        $count = $consume->count();
        $consume->offset( $offset )->limit( $limit );
        $result = $consume->asArray()->all();
        if($result){
            $userModel = new User();
            $user = $userModel->getUserInfo($params['user_id']);
            $nid = array_column( $result , 'novel_id' );
            $novelModel = new Novel();
            $novels = $novelModel->getNovelList( array( 'ids' => $nid ) );
            $n_k_v = array_column( $novels['list'] , null , 'novel_id' );
            unset($novels);
            $chapterModel = new NovelChapter();
            $cid = array_column( $result , 'novel_chapter' );
            $chapters = $chapterModel->getChapterList( array( 'ids' => $cid ) );
            $c_k_v = array_column( $chapters['list'] , null , 'chapter_id' );
            unset($chapters);
            foreach($result as $k=>$v){
                $result[$k]['novel'] = isset($n_k_v[$v['novel_id']]) ? $n_k_v[$v['novel_id']]['novel_title'] : '';
                $result[$k]['chapter'] = isset($c_k_v[$v['novel_chapter']]) ? $c_k_v[$v['novel_chapter']]['chapter_name'] : '';
                $result[$k]['user_nickname'] = isset($user['user_nickname']) ? $user['user_nickname'] : '';
                $result[$k]['user_phone'] = isset($user['user_phone']) ? $user['user_phone'] : '';
            }
        }
        $data['dataProvider'] = new ArrayDataProvider(array(
            'allModels'=> $result,
//            'sort' => [
//                'attributes' => ['userId', 'createTime', 'sex', 'nickname','lastTime'],
//            ],
        ));
        $data['pagination'] = new Pagination(array(
//            'pageSize'          => $limit,
            'defaultPageSize'   => $limit,
            'totalCount'        => $count,
        ));
        return $data;
    }
    
    /**
     * 获取用户消费记录in
     */
    public static function getUserTotal( $params = array() )
    {
        $recharge = self::find();
        $recharge->select('SUM(pay_coin) as money,COUNT(id) as cnt,user_id');
        $recharge->andWhere("status = 1");
        $ids = implode(',',$params['uids']);
        $recharge->andWhere("user_id in ({$ids})");
        $result = $recharge->asArray()->all();
        return $result ? $result : array();
    }
}

